﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserForms
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();

            //启动窗体的时候隐藏窗体
            this.ShowInTaskbar = false;
            this.WindowState = FormWindowState.Minimized;
        }

        public static Form3 StrartForm { get; set; }


        /// <summary>
        /// 初始化窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form3_Load(object sender, EventArgs e)
        {
            StrartForm = this;//存储当前实例

            Form1 enter = new Form1();
            enter.Show();//进入登入窗口


            string sql = "select * from Shop";

            dataGridView1.DataSource = ADOopertion.Select(sql);

            dataGridView1.AllowUserToAddRows = false;//不显示添加新行

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//一行为一个单元格


            groupBox2.Visible = false;

        }

        /// <summary>
        /// 刷新数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            string sql = "select * from Shop";

            dataGridView1.DataSource = ADOopertion.Select(sql);
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text.Length != 0)
            {
                string sql = string.Format("select * from Shop where 商品名称 like '%{0}%'", textBox1.Text);

                dataGridView1.DataSource = ADOopertion.Select(sql);
            }
            else
            {
                MessageBox.Show("请输入要查询的关键字", "提示");
            }

        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            Form4 insertForm = new Form4();
            insertForm.Show();
        }

        /// <summary>
        /// 更新1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            var yesOrNo = MessageBox.Show("你是否已经选择好了要更改的行", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

            if (DialogResult.Yes.Equals(yesOrNo))
            {
                groupBox2.Visible = true;

            }

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {

            if (dataGridView1.SelectedRows.Count == 1)
            {
                var len = MessageBox.Show("确定要删除当前选中的行吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

                if (DialogResult.OK.Equals(len))
                {
                    var id = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value.ToString();

                    string sql = string.Format("delete Shop where id={0}", id);

                 var b=ADOopertion.DeleteUpdateInsert(sql);


                    if (b >= 1)
                    {
                        MessageBox.Show("删除成功！！");

                        string sqlSelect = "select * from Shop";

                        dataGridView1.DataSource = ADOopertion.Select(sqlSelect);
                    }
                }
                
            }
            else
            {
                MessageBox.Show("请选择要删除的行", "提示");
            }
        }

        /// <summary>
        /// 更新2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {
            var id = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["id"].Value;

            if (comboBox1.Text.Length != 0 && textBox2.Text.Length != 0)
            {

                string column = comboBox1.SelectedItem.ToString();

                string values = textBox2.Text.ToString();


                string sql = string.Format("update Shop set {0}='{1}' where id={2}", column, values, id);

                var a = ADOopertion.DeleteUpdateInsert(sql);


                if (column == "价格")
                {
                    var number1 = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["购买数量"].Value.ToString();

                    double allMoney1 = double.Parse(values) * int.Parse(number1);

                    string sql1 = string.Format("update Shop set 消费金额='{0}' where id={1}", allMoney1, id);

                    ADOopertion.DeleteUpdateInsert(sql1);

                }
                else if (column == "购买数量")
                {
                    var number2 = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["价格"].Value.ToString();

                    double allMoney2 = int.Parse(values) * double.Parse(number2);

                    string sql2 = string.Format("update Shop set 消费金额='{0}' where id={1}", allMoney2, id);

                    ADOopertion.DeleteUpdateInsert(sql2);
                }


                if (a >= 1)
                {
                    MessageBox.Show("更新成功");

                    string sqlUpdate = string.Format("select * from Shop where 商品名称 like '%{0}%'", textBox1.Text);

                    dataGridView1.DataSource = ADOopertion.Select(sqlUpdate);

                    comboBox1.SelectedItem = null;

                    textBox2.Clear();

                    groupBox2.Visible = false;
                }

            }
            else
            {
                MessageBox.Show("请填好必要信息");
            }
        }
    }
}
